// pages/goods_list/index.js
// pages/category/index.js
import {request} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[],
    tabs:[{
      id:0,
      value:"综合",
      isActive:true
    },
    {
      id:1,
      value:"销量",
      isActive:false
    },
    {
      id:2,
      value:"价格",
      isActive:false
    }]
  },
  totalPages:0,
  bindtabsItemChange(e){
    console.log(e);
    const
      index
     = e.detail;
    console.log(index);
    let{
      tabs
    } = this.data;
    tabs.forEach((v,i) => i ===index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
    console.log(this.data.tabs);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.QueryParams.cid = this.options.cid || "";
    // this.QueryParams.query = this.options.query || "";
    this.getGoodsList();
  },
  async getGoodsList(){
    var res = await request({
      url:'/goods/search',
      data:this.QueryParams
    })
    const total = res.total;
    this.totalPages=Math.ceil(total / this.QueryParams.pagesize)
    console.log(res);
    this.setData({
      goodsList:[...this.data.goodsList,...res.goods]
    })
    wx.stopPullDownRefresh();
    wx.hideLoading();//隐藏加载中
  },
  QueryParams:{
    // query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      goodsList:[]
    })
    this.QueryParams.pagenum = 1;
    this.getGoodsList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log(1);
    if(this.QueryParams.pagenum>=this.totalPages){
      wx.showToast({
        title:'没有下一页数据'
      })
    }else{
      this.QueryParams.pagenum++;
      this.getGoodsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})